#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <cotp.h>
#include <QPushButton>
#include <QHBoxLayout>
#include <QPlainTextEdit>
#include <QtGui>
#include <QMessageBox>
#include <QLabel>
#include <QPixmap>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
extern int counts;
class MainWindow : public QMainWindow
{
    Q_OBJECT

private:
    QPushButton *zeroBtn;
    QPushButton *oneBtn;
    QPushButton *twoBtn;
    QPushButton *threeBtn;
    QPushButton *fourBtn;
    QPushButton *fiveBtn;
    QPushButton *sixBtn;
    QPushButton *sevenBtn;
    QPushButton *eightBtn;
    QPushButton *nineBtn;
    QPushButton *verifyBtn;
    QPushButton *clearBtn;
    QPushButton *returnBtn;
    QPlainTextEdit *txtEdit;
    QVBoxLayout *VLay,*VLay2;
    QString inputStr = "";
    QLabel *qrDisplay, *test;
    QPlainTextEdit *helpInstruction;
    QImage *qrcode;
    FILE *light_1, *light_2, *light_3, *light_1_dir, *light_2_dir, *light_3_dir, *GPIO_export;
    void initUI();
    void initGPIO();

    void initSignalSlots();
    void genQRCode(std::string);
    char* qrcode_buffer = NULL;
    char* img_buffer = NULL;
    char* totp, *return_totp;
    int qrcode_size;

private slots:
    void setPasswordTextZero() {
        inputStr.append("0");
        txtEdit->setPlainText(inputStr);
    }
    void setPasswordTextOne() {
        inputStr.append("1");
        txtEdit->setPlainText(inputStr);
    }
    void setPasswordTextTwo() {
        inputStr.append("2");
        txtEdit->setPlainText(inputStr);
    }
    void setPasswordTextThree() {
        inputStr.append("3");
        txtEdit->setPlainText(inputStr);
    }
    void setPasswordTextFour() {
        inputStr.append("4");
        txtEdit->setPlainText(inputStr);
    }
    void setPasswordTextFive() {
        inputStr.append("5");
        txtEdit->setPlainText(inputStr);
    }
    void setPasswordTextSix() {
        inputStr.append("6");
        txtEdit->setPlainText(inputStr);
    }
    void setPasswordTextSeven() {
        inputStr.append("7");
        txtEdit->setPlainText(inputStr);
    }
    void setPasswordTextEight() {
        inputStr.append("8");
        txtEdit->setPlainText(inputStr);
    }
    void setPasswordTextNine() {
        inputStr.append("9");
        txtEdit->setPlainText(inputStr);
    }
    void light_down(int num) {
        if (num == 1) {
            //fprintf(light_1,"0\n");
            system("echo 0 > /sys/class/gpio/gpiochip0/gpio23/value");


        } else if (num == 2) {
            //fprintf(light_2,"0\n");
            system("echo 0 > /sys/class/gpio/gpiochip0/gpio24/value");
        } else {
            //fprintf(light_3,"0\n");
            system("echo 0 > /sys/class/gpio/gpiochip0/gpio25/value");
        }
    }
    int light_up(int num) {
        if (num == 1) {
            system("echo 1 > /sys/class/gpio/gpiochip0/gpio23/value");
            return 1;
        } else if (num == 2) {
            system("echo 1 > /sys/class/gpio/gpiochip0/gpio24/value");
            return 2;
        } else if (num == 3) {
            system("echo 1 > /sys/class/gpio/gpiochip0/gpio25/value");
            return 3;
        }
        return 0;
    }
    void verifyPassword() {
        //cotp_error_t *err;
        if (totp_verify ("OF3WK4TUPE", inputStr.toStdString().c_str(), 6, 120, SHA256) == VALID) {
            QMessageBox::about(NULL, "欢迎", "您的板凳已经弹出\n如有故障请联系客服");
            light_down(1);
        } else if (inputStr.toStdString() == "747352") {
            QMessageBox::about(NULL, "欢迎", "您的板凳已经弹出\n如有故障请联系客服");
            light_down(2);
        } else if (inputStr.toStdString() == "334928") {
            QMessageBox::about(NULL, "欢迎", "您的板凳已经弹出\n如有故障请联系客服");
            light_down(3);
        } else {
            QMessageBox::about(NULL, "Oh", "您的输入有误，请尝试重新输入");
            clearPassword(); // no need to preserve a bad code
        }

    }
    void clearPassword() {
        inputStr.clear();
        txtEdit->setPlainText(inputStr);
    }
    int NFC_detected() {
        FILE *fp;
        char buffer[1024];

        fp=popen("nfc-list", "r");
        for (int i=0;i<6;++i) fgets(buffer,sizeof(buffer),fp);
        if (strstr(buffer,"UID (NFCID1): UID1") != NULL) {
            return 1;
        } else if (strstr(buffer,"UID (NFCID1): UID2") != NULL) {
            return 2;
        } else if (strstr(buffer,"UID (NFCID1): UID3") != NULL) {
            return 3;
        } else {
            return 0;
        }
    }
    void return_product() {
        std::string ret_code = "已经成功归还，归还码";
        ret_code += return_totp;
        if (1 | light_up(NFC_detected())) {
            QMessageBox::about(NULL, "谢谢", ret_code.c_str());
        } else {
            QMessageBox::about(NULL, "对不起", "尚未检测到归还\n如果需要帮助请联系客服人员");
        }
    }
    void system(std::string str);
public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
};

#endif // MAINWINDOW_H
